#pragma once
#include "Log.hpp"
#include "Sock.hpp"
#include <cstdint>
#include<iostream>
#include<functional>
#include <netinet/in.h>
#include <pthread.h>
#include<vector>
#include<unistd.h>

namespace ns_tcpserver2
{
    using func_t = std::function<void(int)>;
    class Tcpserver;
    class ThreadData 
    {
    public:
        ThreadData(int sock,Tcpserver* server)
        :_sock(sock)
        ,_server(server)
        {

        }
    public: 
        int _sock;
        Tcpserver* _server;
    };
    class Tcpserver 
    {
    public:
        static void* ThreadRouine(void* args)
        {
            ThreadData* td=static_cast<ThreadData*>(args);
            //td->_server
            td->_server->Execute(td->_sock);
            close(td->_sock);
            return nullptr;
        }
        Tcpserver(uint16_t port,std::string ip)
        {
            _serversock=_sock.Socket();
            _sock.Bind(_serversock, port, ip);
            _sock.Listen(_serversock);
        }
        void BindHandler(func_t callback)
        {
            //这里的每一个方法都要基于sock进行通讯
            _func.push_back(callback);
        }
        void Execute(int sock)
        {
            for(auto& iter:_func){
                iter(sock);
            }
        }
        void start()
        {
            while(true)
            {
                uint16_t client_port;
                std::string client_ip;
                int sock=_sock.Accept(_serversock, &client_port, &client_ip);
                //在这里没有合适的默认构造函数这里就会出现问题
                ThreadData* td=new ThreadData(sock,this);
                pthread_t tid;
                pthread_create(&tid, nullptr, ThreadRoutine, (void*)td);
            }
        }
    private:
        int _serversock;
        Sock _sock;
        std::vector<func_t>_func;
    };
}
namespace ns_tcpserver
{
    using func_t = std::function<void(int)>;
    class TcpServer;
    class ThreadData
    {
    public:
        ThreadData(int sock,TcpServer* server)
        :_sock(sock),_server(server)
        {

        }
        ~ThreadData(){

        }
    public:
        //因为这里存在着自定义类型所以必须使用列表初始化
        int _sock;
        TcpServer* _server;
    };
    class TcpServer 
    {
    public: 
        static void* ThreadRoutine2(void* args)
        {
            pthread_detach(pthread_self());
            ThreadData* td=static_cast<ThreadData*>(args);
            td->_server->Execute(td->_sock);
            close(td->_sock);
            return nullptr;
        }
        static void* ThreadRoutine(void* args)
        {
            pthread_detach(pthread_self());
            ThreadData* td=static_cast<ThreadData*>(args);
            td->_server->Execute(td->_sock);
            close(td->_sock);
            return nullptr;
        }
        TcpServer(uint16_t port,std::string ip)
        {
            _serversock=_sock.Socket();
            _sock.Bind(_serversock, port, ip);
            _sock.Listen(_serversock);
        }
        void BindHandler(func_t callback){
            _funcs.push_back(callback);
        }
        void Execute(int sock)
        {
            for(auto& iter:_funcs){
                iter(sock);
            }
        }
        void Start2()
        {
            while(true)
            {
                uint16_t client_port;
                std::string client_ip;
                int sock=_sock.Accept(_serversock, &client_port, &client_ip);
                if(sock == -1){
                    continue;
                }
                logMessage(NORMAL, "new link finished : %d", sock);
                pthread_t tid;
                ThreadData* td=new ThreadData(sock,this);
                //这个时候就要通过这些静态方法来进行实现了
                pthread_create(&tid, nullptr, ThreadRoutine, (void*)td);
            }
        }
        //要让线程动起来
        void Start()
        {
            while(true)
            {
                uint16_t client_port;
                std::string client_ip;
                int sock=_sock.Accept(_serversock, &client_port, &client_ip);
                if(sock == -1){
                    continue;
                }
                logMessage(NORMAL, "new link success :%d", sock);
                pthread_t tid;
                ThreadData* td=new ThreadData(sock,this);
                pthread_create(&tid, nullptr, ThreadRoutine, (void*)td);
            }
        }
    private:
        int _serversock;
        Sock _sock;
        std::vector<func_t> _funcs;
    };
}
// namespace ns_tcpserver
// {
//     using func_t =std::function<void(int)>;
//     class TcpServer;
//     class ThreadData
//     {
//     public:
//         ThreadData(int sock,TcpServer* server)
//         :_sock(sock),_server(server)
//         {

//         }
//         ~ThreadData(){}
//     public:
//         int _sock;
//         TcpServer* _server;
//     };
//     class TcpServer
//     {
//     public:
//         static void* ThreadRoutine(void* args)
//         {
//             pthread_detach(pthread_self());
//             ThreadData* td=static_cast<ThreadData*>(args);
//             td->_server->Execute(td->_sock);
//             close(td->_sock);
//             return nullptr;
//         }
//     TcpServer(const uint16_t& port,std::string& ip)
//     {
//         _serversock=_sock.Socket();
//         _sock.Bind(_serversock, port, ip);
//         _sock.Listen(_serversock);
//     }
//     void BindHandler(func_t callback)
//     {
//         _func.push_back(callback);
//     }
//     void Execute(int sock)
//     {
//         //这里的函数包装器需要使用到sock
//         for(auto& iter:_func){
//             iter(sock);
//         }
//     }
//     void Start()
//     {
//         while(true)
//         {
//             std::string client_ip;
//             uint16_t client_port;
//             int sock=_sock.Accept(_serversock, &client_port, &client_ip);
//             if(sock == -1){
//                 continue;
//             }
//             logMessage(NORMAL, "create new link success :%d",sock);
//             pthread_t tid;
//             ThreadData* td=new ThreadData(sock,this);
//             pthread_create(&tid, nullptr, ThreadRoutine, (void*)td);
//         }
//     }
//     ~TcpServer(){
//         if(_serversock >= 0){
//             close(_serversock);
//         }
//     }
//     private:
//         int _serversock;
//         Sock _sock;
//         std::vector<func_t> _func;
//     };
// }
